#include "led_test.h"
#include "factory_api.h"

#define DBG_LVL DBG_LOG
#include <rtdbg.h>
static fac_led_res_t factory_led_resource[] = {
    PIN_RUN,
    PIN_BLUE,
    PIN_CAT1,
    PIN_WIFI,
    PIN_ETH,
    PIN_WIOTA,
    PIN_MQTT,
};

#define LED_RESOURCE_SIZE (sizeof(factory_led_resource) / sizeof(factory_led_resource[0]))
void factory_led_init(void)
{
    for (int i = 0; i < LED_RESOURCE_SIZE; i++)
    {
        rt_pin_mode(factory_led_resource[i].pin, PIN_MODE_OUTPUT);
        rt_pin_write(factory_led_resource[i].pin, 0);
    }
}

void led_all_switch(char flag)
{
    for (int i = 0; i < LED_RESOURCE_SIZE; i++)
    {
        rt_pin_write(factory_led_resource[i].pin, flag);
    }
}

void led_all_blink(void)
{
    led_all_switch(0);
    rt_thread_mdelay(1000);
    led_all_switch(1);
    rt_thread_mdelay(1000);
}

void cmd_led_blink(int argc, char **argv)
{
    int i = 0;
    for(i = 0; i < 3; i++)
    {
        rt_kprintf("led start blink\n");
        led_all_blink();
    }
}
MSH_CMD_EXPORT_ALIAS(cmd_led_blink, led_blink, led blink three time);

void save_led_result(int argc, char **argv)
{
    if (argc != 2)
    {
        LOG_D("Usage:led_blink value");
        return;
    }

    if(argv[1][0] == '1')
    {
        factory_test_result(LED_TEST, factory_get_key(LED_TEST), FACTORY_TEST_SUCCESS, "led test success");
    }
    else if(argv[1][0] == '2')
    {
        factory_test_result(LED_TEST, factory_get_key(LED_TEST), FACTORY_TEST_FAIL, "led test fail");
    }
    else
    {
        LOG_D("Please enter the correct value y or n");
    }
    return;
}
MSH_CMD_EXPORT_ALIAS(save_led_result, led_save, save led test result);